import { createStore } from 'vuex'
import menu from './modules/menu'
import role from './modules/role'
import admin from './modules/admin'
let data = sessionStorage.getItem('userinfo')
let token = data ? JSON.parse(data) : ''
export default createStore({
  state: {
    userinfo: token
  },
  getters: {
    getUser(state) {
      return state.userinfo
    }
  },
  mutations: {
    REQ_USER(state, payload) {
      if (payload) {
        state.userinfo = payload
        //把结果存储到会话存储中
        sessionStorage.setItem('userinfo', JSON.stringify(payload))
      } else {
        //清空存储（移除）
        sessionStorage.removeItem('userinfo')

      }
    }
  },
  actions: {
    getUserAction({ commit }, payload) {
      commit('REQ_USER', payload)
    }
  },
  modules: {
    menu,
    role,
    admin
  }
})
